<?php

use com\gdl\kq\interfaces\SetholidaysServiceIf;
use com\gdl\common\dataobject\common\ResultDO;
use com\gdl\kq\dataobject\setholidays\setholidaysDTO;


class api_SetholidaysService extends api_Abstract implements SetholidaysServiceIf
{
	
	//节假日方案列表
	public function getSetholidaysList(setholidaysDTO $setholidaysDTO)
    {
		$result = new ResultDO();
		$data      = $setholidaysDTO->data ? $setholidaysDTO->data : [];
		$id        = $setholidaysDTO->id ? $setholidaysDTO->id : 0;//节假日方案ID
		$platformId = $setholidaysDTO->platformId ? $setholidaysDTO->platformId : 0;//平台ID

				
	    try {
	        $adminReg = $data['adminReg'];
	        $agId = $data['agId'];//考勤组ID
	        $adminuserid = $data['adminuserid'];
            $condition = [];
                if ($id){
                    $condition['id'] = $id;
                    $setholidaysModel = new model_attendance_setholidays();
                    $res = $setholidaysModel->select($condition)->items;
                    $result->data = $res;
                    if(!empty($res[0])){
                        $result->code = 1;
                    } else {
                        $result->code = 0;
                    }

                    $result->success = true;
                }else{
                    if ($agId){
                        $attendancegroupCondition = ['id'=>$agId];
                        $attendancegroupModel = new model_attendance_attendancegroup();
                        $setholidaysId = $attendancegroupModel->selectOne($attendancegroupCondition,'id,setholidaysId');
                        $setholidaysId = $setholidaysId['setholidaysId'];
                        $condition['id'] = $setholidaysId;
                    }
                    if ($platformId){
                        $condition['platformId'] = $platformId;
                    }

                    $setholidaysModel = new model_attendance_setholidays();
                    $res = $setholidaysModel->select($condition)->items;
                    $result->data = $res;
                    if(!empty($res[0])){
                        $result->code = 1;
                    } else {
                        $result->code = 0;
                    }

                    $result->success = true;
                }
            } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage();
            }
            return $result;
	}


    //添加节假日方案
    public function addSetholidays(setholidaysDTO $setholidaysDTO)
    {
        $result = new ResultDO();
        $data      = $setholidaysDTO->data ? $setholidaysDTO->data : [];
        $platformId = $setholidaysDTO->platformId ? $setholidaysDTO->platformId : 0;//平台ID


        try {
            $setholidaysModel = new model_attendance_setholidays();
            //查询节假日方案名是否重复
                $name = $setholidaysModel->selectOne(['platformId'=>$platformId,'name'=>$data['name']],'id');
                if ($name){
                    $result->code = 0;
                    $result->message = "节假日方案名:（{$name}）重复，请换一个方案名！";
                }else{
                    $id = $setholidaysModel->insert($data);
                    $result->data = [[$id]];
                    if(!empty($id)){
                        $result->code = 1;
                        $result->message = "方案添加成功！";
                    } else {
                        $result->code = 0;
                        $result->message = "方案添加失败！";
                    }
                }
            $result->success = true;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }
    //操作节假日方案
    public function operationSetholidays(setholidaysDTO $setholidaysDTO)
    {
        $result = new ResultDO();
        $data      = $setholidaysDTO->data ? $setholidaysDTO->data : [];
        $platformId = $setholidaysDTO->platformId ? $setholidaysDTO->platformId : 0;//平台ID
        $id = $setholidaysDTO->id ? $setholidaysDTO->id : 0;//平台ID


        try {
            $do = $data['do'];
            unset($data['do']);
            $setholidaysModel = new model_attendance_setholidays();
            if ($do==1){//1添加 2删除
                //查询节假日方案名是否重复
                $name = $setholidaysModel->selectOne(['platformId'=>$platformId,'name'=>$data['name']],'id');
                if ($name){
                    $result->code = 0;
                    $result->message = "节假日方案名:（{$name}）重复，请换一个方案名！";
                }else{
                    $id = $setholidaysModel->insert($data);
                    $result->data = [[$id]];
                    if(!empty($id)){
                        $result->code = 1;
                        $result->message = "方案添加成功！";
                    } else {
                        $result->code = 0;
                        $result->message = "方案添加失败！";
                    }
                }
            }else{
                $setholidaysModel = new model_attendance_setholidays();
                $setholidaysModel->update(" id={$id} ",$data);
                $result->data = [[$id]];
                if(!empty($id)){
                    $result->code = 1;
                    $result->message = "方案修改成功！";
                } else {
                    $result->code = 0;
                    $result->message = "方案修改失败！";
                }
            }

            $result->success = true;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }

    //修改节假日方案
    public function editSetholidays(setholidaysDTO $setholidaysDTO)
    {
        $result = new ResultDO();
        $data      = $setholidaysDTO->data ? $setholidaysDTO->data : [];
        $id      = $setholidaysDTO->id ? $setholidaysDTO->id : 0;
        $platformId = $setholidaysDTO->platformId ? $setholidaysDTO->platformId : 0;//平台ID


        try {
            $setholidaysModel = new model_attendance_setholidays();
            $setholidaysModel->update(" id={$id} ",$data);
            $result->data = [[$id]];
            if(!empty($id)){
                $result->code = 1;
                $result->message = "方案修改成功！";
            } else {
                $result->code = 0;
                $result->message = "方案修改失败！";
            }
            $result->success = true;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }

    //删除节假日方案
    public function deleteSetholidays(setholidaysDTO $setholidaysDTO)
    {
        $id = $setholidaysDTO->id ? gdl_lib_BaseUtils::getStr($setholidaysDTO->id) : 0;
        $result = new ResultDO();
        $result->success = true;
        $result->message = "您已删除，请勿重复操作";
        $result->code = 400;
        $result->data = [];
//        if(empty($platformId)){
//            $result->message = "您还没有选择平台";
//            return $result;
//        }
        if(empty($id)){
            $result->message = "参数错误";
            return $result;
        }
        try{
            $setholidaysModel = new model_attendance_setholidays();
            $attendancegroupModel = new model_attendance_attendancegroup();
            //考勤组查询是否有该节假日选择
            $ty = $attendancegroupModel->selectOne("setholidaysId = {$id} and isDelete=0");

            if ($ty){
                $result->code = 400;
                $result->message = "删除失败,考勤组{$ty['title']}已经设置该节假日,调整后删除";
            }else{
                $where = " id = {$id} ";
                $res = $setholidaysModel->delete($where);
                if($res){
                    $result->code = 200;
                    $result->message = "已删除";
                }
            }
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }
}

